<?php

namespace app\admin\controller\official;

use app\common\controller\Backend;
use think\Db;

/**
 * 投保管理
 *
 * @icon fa fa-user
 */
class Insure extends Backend
{

    protected $relationSearch = true;


    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
   
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
			$conn = Db::connect(config('database.pajk'));
            $total = $conn->name('insure_order')
                ->where($where)
                ->order($sort, $order)
                ->field('id')
                ->count();
            $list = $conn->name('insure_order')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                // ->field('id,last_login,mobile,login_num,last_ip,openid,create_time')
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
	public function detail($ids){
	    $this->relationSearch = true;
	
	    //设置过滤方法
	    $this->request->filter(['strip_tags']);
	    $detail = Db::name('integral_order')->alias('a')
			->where('a.id',$ids)
			->leftJoin('areas b','a.province = b.id')
			->leftJoin('areas c','a.city = c.id')
			->leftJoin('areas d','a.county = d.id')
			->field('a.*,b.name as province_name,c.name as city_name,d.name as county_name')
			->find();
		// dump(Db::name('integral_order')->getLastSql());
		//查找商品信息
		$goodsInfo = Db::name('integral_order_goods')->where('order_id',$detail['id'])->select();
	    // dump($detail);exit;
	    $this->view->assign("row", $detail);
	    $this->view->assign("goodsInfo", $goodsInfo);
	    return $this->view->fetch();
	}
	public function deliver($ids){
		$conn = Db::connect(config('database.pajk'));
		$row = $conn->name('insure_order')->where('id',$ids)->find();
		if ($this->request->isPost()) {
			$params = $this->request->post("row/a");
			//判断入住日期是否存在
			$orderInfo = $conn->name('insure_order')->where('reservation_date',$params['reservation_date'])->find();
			if ($orderInfo) {
				$this->error('入住日期已存在！');
			}
			if ($params) {
			    $params = $this->preExcludeFields($params);
			    $result = false;
			    Db::startTrans();
			    try {
			        //先修改商品主表
			        $data['reservation_date'] = $params['reservation_date'];
			        $data['is_reservation'] = 2;
			        $result = $conn->name('insure_order')->where('id',$row['id'])->update($data);
			        Db::commit();
			    }catch (Exception $e) {
			        Db::rollback();
			        $this->error($e->getMessage());
			    }
			    if ($result !== false) {
			        $this->success();
			    } else {
			        $this->error(__('No rows were updated'));
			    }
			}
			$this->error(__('Parameter %s can not be empty', ''));
		}else{
			// $detail = $this->model->find($ids);
			$this->view->assign("row", $row);
			return $this->view->fetch();
		}
	}
}
